package cn.kwq.project_manger.service.impl;

import cn.hutool.core.util.StrUtil;
import cn.kwq.project_manger.pojo.*;
import cn.kwq.project_manger.pojo.dto.PageDto;
import cn.kwq.project_manger.repostitory.InstituteDicRepository;
import cn.kwq.project_manger.repostitory.MajorDicRepository;
import cn.kwq.project_manger.service.DicService;
import com.querydsl.jpa.impl.JPAQueryFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class DicServiceImpl implements DicService {

    @Autowired
    JPAQueryFactory jpaQueryFactory;

    @Autowired
    InstituteDicRepository instituteDicRepository;

    @Autowired
    MajorDicRepository majorDicRepository;



    @Override
    public PageDto searchInstituteDic(String dicName,Integer pageSize,Integer pageNum) {
        QInstituteDic instituteDic=QInstituteDic.instituteDic;

        if (dicName==null|| StrUtil.hasBlank(dicName)){
            List<InstituteDic> fetch = jpaQueryFactory
                    .selectFrom(instituteDic)
                    .offset((long) (pageNum - 1) * pageSize)
                    .limit(pageSize)
                    .fetch();
            Long total = jpaQueryFactory
                    .select(instituteDic.instituteId.count())
                    .from(instituteDic)
                    .fetchOne();
            return PageDto.builder()
                    .pageDate(fetch)
                    .total(total)
                    .build();

        }

        List<InstituteDic> fetch = jpaQueryFactory
                .selectFrom(instituteDic)
                .where(instituteDic.instituteName.like("%" + dicName + "%"))
                .offset((long) (pageNum - 1) * pageSize)
                .limit(pageSize)
                .fetch();

        if (fetch.isEmpty()||fetch.get(0)==null){
            return null;
        }

        Long total = jpaQueryFactory
                .select(instituteDic.instituteId.count())
                .from(instituteDic)
                .where(instituteDic.instituteName.like("%" + dicName + "%"))
                .fetchOne();



        return PageDto.builder()
                .pageDate(fetch)
                .total(total)
                .build();
    }

    @Override
    public PageDto searchMajorDic(String dicName, Integer pageSize, Integer pageNum) {

        if (dicName==null|| StrUtil.hasBlank(dicName)){
            PageRequest page = PageRequest.of(pageNum,pageSize);
            Page<MajorDic> all = majorDicRepository.findAll(page);
            int totalPages = all.getTotalPages();
            return PageDto.builder()
                    .pageDate(all.getContent())
                    .total((long) totalPages)
                    .build();
        }

        QMajorDic majorDic=QMajorDic.majorDic;

        List<MajorDic> fetch = jpaQueryFactory
                .selectFrom(majorDic)
                .where(majorDic.majorName.like("%" + dicName + "%"))
                .offset((long) (pageNum - 1) * pageSize)
                .limit(pageSize)
                .fetch();

        if (fetch.isEmpty()||fetch.get(0)==null){
            return null;
        }

        Long total = jpaQueryFactory
                .select(majorDic.majorId.count())
                .from(majorDic)
                .where(majorDic.majorName.like("%" + dicName + "%"))
                .fetchOne();


        return PageDto.builder()
                .pageDate(fetch)
                .total(total)
                .build();
    }
}
